# coding:utf-8

import socket

from multiprocessing import Process

import multiprocessing as mul

import datetime as dt

# 获取当前时间
def time_service(pipe):

    while True:
        print("1waiting for server's command>>>")

        start_info=pipe.recv()

        print(start_info)

        print("1getting currunt system time>>>>>>>>")

        now_time = dt.datetime.now().strftime('%F %T')

        # 输出时间

        print("1starting send time message to server>>>>>>>>")

        pipe.send(now_time)

    #return now_time

def get_one_package(client_socket):
    head_signal = False

    tail_signal = False

    info = bytearray(0)

    while True:



        SIGNAL = client_socket.recv(1)
        if SIGNAL[0] == 40:
            print("find package start symbol>>>>>>>>")
            head_signal = True
        if head_signal == True:
            info.append(SIGNAL[0])
        if SIGNAL[0] == 41:
            print("find package end symbol>>>>>>>")
            tail_signal = True
        if  tail_signal :
            print("get single data package>>>>>")
            break
    #print(info)
    return info


def handle_client(client_socket,pipe):
    """
    处理客户端请求
    """
    while True:
        print("2reciving message from client>>>>>>>")
        #request_data = client_socket.recv(1024)
        request_data=get_one_package(client_socket)
        print("2request data:", request_data)
        print("2quering time to time_service>>>>>>>")
        pipe.send(request_data)
        print("waiting for response>>>>>>>")
        #client_socket.send(bytes("response", "utf-8"))
        time_result=pipe.recv()
        #print('2proc1 rec:', pipe.recv())
        print("2sengding query resylt to server")
        client_socket.send(bytes(time_result,"utf-8"))



def server(pipe):
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind(("", 4779))
    server_socket.listen(128)

    while True:
        client_socket, client_address = server_socket.accept()
        print(client_socket)
        print("[%s, %s]用户连接上了" % client_address)
        handle_client_process = Process(target=handle_client, args=(client_socket,pipe))
        handle_client_process.start()


import time
if __name__ == "__main__":
    #server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #server_socket.bind(("", 8000))
    #server_socket.listen(128)
    #b="asdasd"
    #print(b[:-3])
    time_ = time.strftime('%Y-%m-%d_%H:%M:%S')
    #print(len(time_))
    region_num="3"
    model_num="123"
    msg=str(region_num) + time_ + str(model_num)
    msg_len=len(msg)
    #print(len(msg))
    #print(len(msg))
    region_num=int(msg[:-22])
    time_crr=msg[-22:-3]
    model_num=int(msg[-3:])
    print(model_num)

    print(region_num)
    print(time_crr)